#!python3
# -*- coding: utf-8 -*-

import time, json, requests

class WeChat2group(object):
    def __init__(self, title, description):
        self.url = "https://qyapi.weixin.qq.com"
        self.corpid = "xxx"
        self.secret = "xxxxFda3YeYGJOleTrz7JhlbPPy0"
        self.agentid = "1000002"
        self.title = title
        self.description = description


    def access_token(self):
        url_arg = '/cgi-bin/gettoken?corpid={id}&corpsecret={crt}'.format(
            id=self.corpid, crt=self.secret)
        url = self.url + url_arg
        response = requests.get(url=url)
        text = response.text
        self.token = json.loads(text)['access_token']

    def messages(self,title,description):
        values = {
            "chatid": "appoMonitor",
            "msgtype": "markdown",
            "markdown" : {
                "content": title+"\n"+description
            },
            "enable_id_trans": 1
        }
        self.msg = json.dumps(values)
        return self.msg

    def sendMessage(self):
        accessToken = self.token
        title = self.title
        description = self.description
        msg = self.messages(title,description)
        send_url = '{url}/cgi-bin/appchat/send?access_token={token}'.format(url=self.url, token=accessToken)
        response = requests.post(url=send_url, data=msg)
        errcode = json.loads(response.text)['errcode']
        if errcode == 0:
            print('Success')
        else:
            print('Failed')